import Vue from 'vue'
import VueRouter from 'vue-router'
/* import login from '../views/login/index.vue'
import layout from '../views/layout/index.vue'
// 主页中各个页面
import welcome from '../views/layout/welcome.vue' // 欢迎页面
import chart from '../views/layout/chart/index.vue' // 数据预览
import user from '../views/layout/user/index.vue' // 用户列表
import enterprise from '../views/layout/enterprise/index.vue' // 企业信息
import question from '../views/layout/question/index.vue' // 题库列表
import subject from '../views/layout/subject/index.vue' // 学科列表 */

import father from '@/slotStudy/father.vue' // 学习插槽导入的页面，与项目无关

// 路由懒加载
const login = () => import('../views/login/index.vue')
const layout = () => import('../views/layout/index.vue')
const welcome = () => import('../views/layout/welcome.vue')
const chart = () => import('../views/layout/chart/index.vue')
const user = () => import('../views/layout/user/index.vue')
const enterprise = () => import('../views/layout/enterprise/index.vue')
const question = () => import('../views/layout/question/index.vue')
const subject = () => import('../views/layout/subject/index.vue')

Vue.use(VueRouter)
const routes = [
  {
    name: 'login',
    path: '/login',
    component: login,
    meta: {
      title: '登录页面'
    }
  },
  {
    name: 'layout',
    path: '/layout',
    component: layout,
    children: [
      {
        name: 'welcome',
        path: 'welcome',
        component: welcome,
        meta: {
          title: '欢迎页面', // 左侧的标题
          icon: 'el-icon-goods', // 图标
          path: '/layout/welcome', // 访问的路径
          roles: ['超级管理员', '管理员', '老师', '学生'] // 哪些角色可以访问
        }
      },
      {
        name: 'chart',
        path: 'chart',
        component: chart,
        meta: {
          title: '数据预览',
          icon: 'el-icon-pie-chart',
          path: '/layout/chart',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        name: 'user',
        path: 'user',
        component: user,
        meta: {
          title: '用户列表',
          icon: 'el-icon-user',
          path: '/layout/user',
          roles: ['超级管理员', '管理员']
        }
      },
      {
        name: 'enterprise',
        path: 'enterprise',
        component: enterprise,
        meta: {
          title: '企业列表',
          icon: 'el-icon-office-building',
          path: '/layout/enterprise',
          roles: ['超级管理员']
        }
      },
      {
        name: 'question',
        path: 'question',
        component: question,
        meta: {
          title: '题库列表',
          icon: 'el-icon-edit-outline',
          path: '/layout/question',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        name: 'subject',
        path: 'subject',
        component: subject,
        meta: {
          title: '学科列表',
          icon: 'el-icon-notebook-2',
          path: '/layout/subject',
          roles: ['超级管理员', '管理员', '老师']
        }
      }
    ]
  },
  {
    // 这个是对应slotStudy练习插槽用的
    name: 'father',
    path: '/father',
    component: father
  },
  {
    path: '/',
    redirect: '/login' // 重定向
  }
]

const router = new VueRouter({
  routes
})

// 前置导航守卫
// 每次路由跳转都是执行
// to 去哪里
// from 从哪里来
// next 跳转到哪里
const whiteList = ['/login'] // 白名单
router.beforeEach((to, from, next) => {
  // includes方法用于判断字符串是否包含指定的子字符串
  if (whiteList.includes(to.path)) {
    next()
  } else {
    const token = localStorage.getItem('heimamm-token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

// 后置导航守卫
router.afterEach((to, from) => {
  document.title = to.meta.title || '黑马面面'
})
export default router
